<template>
  <div class="login-container">
    <div class="login_title">资源采集活动管理系统</div>
    <div class="loginbox">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
        label-position="left">
        <div class="title-container flexcenter">
          <!-- <img src="@/assets/logo.png" alt=""> -->
          <h3 class="title">用户登录</h3>
        </div>
        <el-form-item prop="username">
          <el-input ref="username" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text"
            tabindex="1" auto-complete="off">
            <svg-icon slot="prefix" icon-class="user1" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
            placeholder="请输入密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="lock1" class="el-input__icon input-icon" />
          </el-input>
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-button :loading="loading" class="subbtn" @click.native.prevent="handleLogin">登 录</el-button>
        <!-- <div class="tips">
          <span style="margin-right:20px;">username: admin</span>
          <span> password: any</span>
        </div> -->
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度不能少于六位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: '/home' })
            this.loading = false
          }).catch((error) => {
            // console.log('失敗',error);
            // this.$message.error(error.message)
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #283443;
$light_gray: #fff;
$cursor: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.el-input__inner {
  border: 0;
  border-bottom: 1px solid #DCDFE6;
  border-radius: 0;
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 44px;
    width: 100%;

    input {
      background: transparent;
      padding: 12px 35px 12px 45px;
      color: #333;
      height: 44px;
      caret-color: $cursor;

      &:-webkit-autofill {
        background-color: transparent;
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }

    .el-input__prefix {
      left: 20px;
      line-height: 44px;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login_title {
  position: absolute;
  top: 20px;
  right: 40px;
  font-size: 28px;
  color: #000;
  letter-spacing: 2px;
}

.login-container {
  min-height: 100%;
  width: 100%;
  background: url('~@/assets/images/loginbg.png') no-repeat;
  background-size: cover;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: flex-end;

  .loginbox {
    background-color: white;
    width: 420px;
    padding: 25px 36px;
    margin-right: 16%;
    border-radius: 10px;
    // margin-left: 56%;
  }

  .login-form {
    position: relative;
    width: 100%;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .title-container {
    position: relative;
    margin-bottom: 40px;

    img {
      width: 46px;
      height: 46px;
      margin-right: 20px;
    }

    .title {
      font-size: 24px;
      // margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      color: #000;
    }
  }

  .subbtn {
    margin: 25px 0;
    height: 40px;
    width: 100%;
    border-radius: 20px;
    font-size: 16px;
    // background: linear-gradient(180deg, #E1874A 0%, #1786FB 2%, #1353FF 100%);
    border: 0;
    color: white;
    background: linear-gradient(180deg, #E1874A 0%, #FB9141 2%, #F95E4F 100%);
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

@media (max-width: 420px) {
  .loginbox {
    margin-right: 2% !important;
    margin-left: 2%;
  }
}
</style>